#!/bin/bash

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# -*- sh-basic-offset: 4 -*-

set -exuo pipefail

DEVICE=linux-lubancat-g++
SRC_PATH=/opt
BUILD_TARGET=/build

#Qt源码包名称，默认是Qt6.2.4，，交叉编译器默认是8.3
PACKAGE_NAME=qt-everywhere-src-6.2.4
CROSS_COMPILER=gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu

#定义编译后安装--生成的文件,文件夹位置路径
HOST_BUILD_PATH=${SRC_PATH}/qt6-hostbuild                   # Qt6 host 编译目录
HOST_INSTALL_PATH=${SRC_PATH}/qt6-host                      # Qt6 host 安装目录

TARGET_BUILD_PATH=${SRC_PATH}/qt6-lubancat                       # Qt6 target 编译目录
TARGET_INSTALL_PATH=${BUILD_TARGET}/lubancat_qt6_debian10     # Qt6 target 安装目录

# Toolchain File
CMAKE_TOOLCHAIN_FILE=${SRC_PATH}/lubancat_qt6.cmake

#无需修改--自动组合平台路径
CONFIG_PATH=${SRC_PATH}/${PACKAGE_NAME}/qtbase/mkspecs/devices/${DEVICE}

#无需修改--自动组合配置平台路径文件
CONFIG_FILE=${CONFIG_PATH}/qmake.conf

Fetch_Src(){
	echo "Install Cross Compiler / Qt6.2.4 Source..."
	pushd ${SRC_PATH}
	
	if [ ! -d "${PACKAGE_NAME}" ];then
		wget -c https://download.qt.io/official_releases/qt/6.2/6.2.4/single/qt-everywhere-src-6.2.4.tar.xz
		tar -xf qt-everywhere-src-6.2.4.tar.xz
	fi

	if [ ! -d "${CROSS_COMPILER}" ];then
		wget -c wget https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz
		tar -xf gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz
	fi

	echo "done..."
	popd

}

Install_Cmake(){
 	version=$(cmake --version | grep -oE '[^ ]+$' | head -n 1)
	major_minor=$(echo "$version" | cut -d'.' -f1-2)  
	if [[ "${major_minor}" < "3.18" ]]; then 
		echo "start install cmake ..."
		pushd ${SRC_PATH}
		git clone https://github.com/Kitware/CMake.git
		pushd CMake/
		./bootstrap
		make && make install
		
		hash -r
		echo "done..."
		popd
		popd
	fi
}

# 编译主机qt，编译后安装在/opt/qt6-hostbuild
Building_Host(){
	echo "Building Qt 6 for Host Machine..."
	if [ ! -d "${HOST_INSTALL_PATH}" ];then
		mkdir ${HOST_BUILD_PATH} ${HOST_INSTALL_PATH}
		pushd ${HOST_BUILD_PATH}
		cmake ${SRC_PATH}/${PACKAGE_NAME}/  -GNinja -DCMAKE_BUILD_TYPE=Release -DQT_BUILD_EXAMPLES=OFF -DQT_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=${HOST_INSTALL_PATH}
		cmake --build . --parallel 12
		cmake --install .
		popd
	fi
	echo "done..."
}

# 修理一些出现的问题,
# https://bugreports.qt.io/browse/QTBUG-99295
Patch_Src(){
	pushd ${SRC_PATH}/${PACKAGE_NAME}
	sed -i 's/qt_internal_add_tool/qt_internal_add_app/g' qtconnectivity/src/tools/sdpscanner/CMakeLists.txt
	popd
}

#创建和修改配置平台
Config_Before () {
	echo "start configure platform..."
	pushd ${SRC_PATH}/${PACKAGE_NAME}
	
	if [ ! -d "${CONFIG_PATH}" ];then
		cp -a ${SRC_PATH}/${PACKAGE_NAME}/qtbase/mkspecs/devices/linux-generic-g++ ${CONFIG_PATH}
	fi

	echo "#" > ${CONFIG_FILE}
	echo "# qmake configuration for the LubanCat running Linux for debian10 " >> ${CONFIG_FILE}
	echo "#" >> ${CONFIG_FILE}
	echo "" >> ${CONFIG_FILE}
	echo "include(../common/linux_device_pre.conf)" >> ${CONFIG_FILE}
	echo "" >> ${CONFIG_FILE}
	echo "QMAKE_LIBS_EGL         += -lEGL -lmali" >> ${CONFIG_FILE}
	echo "QMAKE_LIBS_OPENGL_ES2  += -lGLESv2 -lEGL -lmali" >> ${CONFIG_FILE}
	echo "QMAKE_CFLAGS            = -march=armv8-a" >> ${CONFIG_FILE}
	echo "QMAKE_CXXFLAGS          = \$\$QMAKE_CFLAGS" >> ${CONFIG_FILE}
	echo "QMAKE_LFLAGS += -static-libstdc++ -ldbus-1" >> ${CONFIG_FILE}
	echo "" >> ${CONFIG_FILE}
	
	echo "QMAKE_INCDIR_POST += \
		\$\$[QT_SYSROOT]/usr/include \
		\$\$[QT_SYSROOT]/usr/include/\$\${GCC_MACHINE_DUMP} " >> ${CONFIG_FILE}

	echo "QMAKE_LIBDIR_POST += \
		\$\$[QT_SYSROOT]/usr/lib \
		\$\$[QT_SYSROOT]/lib/\$\${GCC_MACHINE_DUMP} \
		\$\$[QT_SYSROOT]/usr/lib/\$\${GCC_MACHINE_DUMP} " >> ${CONFIG_FILE}

	echo "QMAKE_RPATHLINKDIR_POST += \
		\$\$[QT_SYSROOT]/usr/lib \
		\$\$[QT_SYSROOT]/usr/lib/\$\${GCC_MACHINE_DUMP} \
		\$\$[QT_SYSROOT]/lib/\$\${GCC_MACHINE_DUMP} " >> ${CONFIG_FILE}
	
	echo "" >> ${CONFIG_FILE}
	echo "DISTRO_OPTS += aarch64" >> ${CONFIG_FILE}
	echo "DISTRO_OPTS += deb-multi-arch" >> ${CONFIG_FILE}
	echo "" >> ${CONFIG_FILE}
	echo "include(../common/linux_arm_device_post.conf)" >> ${CONFIG_FILE}
	echo "load(qt_config)" >> ${CONFIG_FILE}

	cat ${CONFIG_FILE}
	echo "done..."
	popd
}

# 交叉编译qt,默认使用cmake命令编译，也可以使用/opt/t-everywhere-src-6.2.4/configure
Building_Target(){

	echo "start configure platform..."
	
	if [ ! -d "${TARGET_BUILD_PATH}" ];then
	mkdir ${TARGET_BUILD_PATH}
	fi
	
	pushd ${TARGET_BUILD_PATH}
	cmake ${SRC_PATH}/${PACKAGE_NAME}/ -GNinja -DCMAKE_BUILD_TYPE=Release  -DINPUT_opengl=es2 \
	-DQT_BUILD_EXAMPLES=OFF -DQT_BUILD_TESTS=OFF -DQT_HOST_PATH=${HOST_INSTALL_PATH}  -DCMAKE_STAGING_PREFIX=${TARGET_INSTALL_PATH} \
	-DCMAKE_INSTALL_PREFIX=${TARGET_INSTALL_PATH} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}  -DQT_QMAKE_TARGET_MKSPEC=devices/${DEVICE} \
	-DQT_FEATURE_xcb=ON -DFEATURE_xcb_xlib=ON -DQT_FEATURE_xlib=ON -DFEATURE_qtwebengine_build=OFF
	
	cmake --build . --parallel 12
	cmake --install .
	
	popd
	
	tar cfz "lubancat_qt6_debian10.tar.gz" ${TARGET_INSTALL_PATH}
	sha256sum "lubancat_qt6_debian10.tar.gz" > "lubancat_qt6_debian10.tar.gz.sha256"
}

do_delete_file () {
	pushd ${SRC_PATH}
	
	if [ -f "${HOST_BUILD_PATH}" ];then
		rm -rf ${HOST_BUILD_PATH}
	fi
	
	if [ -f "${TARGET_BUILD_PATH}" ];then
		rm -rf ${TARGET_BUILD_PATH}
	fi
	
	popd
}

# 获取Qt源码/交叉编译器
Fetch_Src

# 安装cmake
Install_Cmake

# 编译主机qt
Building_Host

# 修理一些BUG
Patch_Src

# 配置环境
Config_Before

# 交叉编译qt
Building_Target

# 删除文件
#delete_file



